package com.ruoyi.training.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.training.domain.Remarks;
import com.ruoyi.training.dto.payment.PaymentInvoiceDto;
import com.ruoyi.training.service.IRemarksService;
import com.ruoyi.training.vo.payment.ChangePayVo;
import com.ruoyi.training.vo.payment.SelectPayVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.training.domain.PaymentRecord;
import com.ruoyi.training.service.IPaymentRecordService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 缴费记录Controller
 *
 * @author hcc
 * @date 2025-09-28
 */
@RestController
@RequestMapping("/record")
public class PaymentRecordController extends BaseController
{
    @Autowired
    private IPaymentRecordService paymentRecordService;

    @Autowired
    private IRemarksService remarksService;
    /**
     * 查询缴费记录列表
     */
    @RequiresPermissions("training:record:list")
    @GetMapping("/list")
    public TableDataInfo list(PaymentRecord paymentRecord)
    {
        startPage();
        List<PaymentRecord> list = paymentRecordService.selectPaymentRecordList(paymentRecord);
        return getDataTable(list);
    }

    /**
     * 导出缴费记录列表
     */
    @RequiresPermissions("training:record:export")
    @Log(title = "缴费记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PaymentRecord paymentRecord)
    {
        List<PaymentRecord> list = paymentRecordService.selectPaymentRecordList(paymentRecord);
        ExcelUtil<PaymentRecord> util = new ExcelUtil<PaymentRecord>(PaymentRecord.class);
        util.exportExcel(response, list, "缴费记录数据");
    }

    /**
     * 获取缴费记录详细信息
     */
    @RequiresPermissions("training:record:query")
    @GetMapping(value = "/{paymentRecordId}")
    public AjaxResult getInfo(@PathVariable("paymentRecordId") String paymentRecordId)
    {
        return success(paymentRecordService.selectPaymentRecordByPaymentRecordId(paymentRecordId));
    }

    /**
     * 新增缴费记录
     */
    @RequiresPermissions("training:record:add")
    @Log(title = "缴费记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PaymentRecord paymentRecord)
    {
        return toAjax(paymentRecordService.insertPaymentRecord(paymentRecord));
    }

    /**
     * 修改缴费记录
     */
    @RequiresPermissions("training:record:edit")
    @Log(title = "缴费记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PaymentRecord paymentRecord)
    {
        if (StringUtils.isNotEmpty(paymentRecord.getExampleOne()) && StringUtils.isNotEmpty(paymentRecord.getExampleTwo())){
            Remarks remarks = new Remarks();
            remarks.setForeignId(paymentRecord.getPaymentRecordId());
            remarks.setRemarkType(paymentRecord.getExampleTwo());
            remarks.setRemarks(paymentRecord.getExampleOne());
            remarksService.insertRemarks(remarks);
        }
        return toAjax(paymentRecordService.updatePaymentRecord(paymentRecord));
    }

    /**
     * 删除缴费记录
     */
    @RequiresPermissions("training:record:remove")
    @Log(title = "缴费记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{paymentRecordIds}")
    public AjaxResult remove(@PathVariable String[] paymentRecordIds)
    {
        return toAjax(paymentRecordService.deletePaymentRecordByPaymentRecordIds(paymentRecordIds));
    }

    /**
     * 查询缴费记录列表
     */
    @RequiresPermissions("training:record:list")
    @GetMapping("/paymentRecordList")
    public TableDataInfo paymentRecordList(SelectPayVo selectPayVo)
    {
        List<PaymentInvoiceDto> list = paymentRecordService.getPaymentRecordList(selectPayVo);
        return getDataTable(list);
    }

    /**
     * 批量修改缴费状态
     */
    @RequiresPermissions("training:record:edit")
    @Log(title = "缴费记录", businessType = BusinessType.UPDATE)
    @PutMapping("/changePay")
    public AjaxResult changePay(@RequestBody ChangePayVo changePayVo)
    {
        return toAjax(paymentRecordService.changePay(changePayVo));
    }

    /**
     * 获取缴费记录详细信息
     */
    @RequiresPermissions("training:record:query")
    @GetMapping(value = "/getPayInfo/{paymentRecordId}")
    public AjaxResult getPayInfo(@PathVariable("paymentRecordId") String paymentRecordId)
    {
        return success(paymentRecordService.selectByPaymentRecordId(paymentRecordId));
    }

    /**
     * 获取退费申请详细信息
     */
    @RequiresPermissions("training:record:query")
    @GetMapping(value = "/getRefundInfo/{paymentRecordId}")
    public AjaxResult getRefundInfo(@PathVariable("paymentRecordId") String paymentRecordId)
    {
        return success(paymentRecordService.selectRefundInfo(paymentRecordId));
    }

    /**
     * 查询我的缴费记录列表
     */
    @RequiresPermissions("training:record:list")
    @GetMapping("/myPaymentRecordList")
    public TableDataInfo myPaymentRecordList(SelectPayVo selectPayVo)
    {
        List<PaymentInvoiceDto> list = paymentRecordService.myPaymentRecordList(selectPayVo);
        return getDataTable(list);
    }

}
